﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FinosLibrary;
using FinosLibrary.Client;

using System.IO.IsolatedStorage;
namespace BattleShipOnline {
    [MessageProcess]
    public class Login:ModelLogic {
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool usePassword { get; set; }
        private string _state;
        public string state {
            get { return _state; }
            set { _state = value; this.NotifyPropertyChanged(x => x.state); }
        }
        [AutoWire]
        public UserModel userModel;
        [MessageMethod]
        public void ConnectAndLogin() {
            state = "waiting";
            CNetConnection.getInstance().Connect(Config.instance.server, Config.instance.connection);
        }
        [MessageMethod]
        public void Login_Success(User user) {
            userModel.self = user;
            onSuccess();
        }
        [MessageMethod]
        public void Regist_Success(User user) {
            userModel.self = user;
            onSuccess();
        }
        private void onSuccess() {
            var custom = Custom.GetCustom();
            custom.name = UserName;
            custom.password = Password;
            custom.usePassword = usePassword;
            custom.upDate();
            state = "success";
        }
        [MessageMethod]
        public void Login_Faild(string reason) {
            state = "normal";
            MessageBox.Show(reason, "登录失败！", MessageBoxButton.OK);
        }
        [MessageMethod]
        public override void NetConnection_Connected() {
            base.NetConnection_Connected();
            call("UserLogin", new string[] { UserName, usePassword ? Password : "" });
        }
        [MessageMethod]
        public override void NetConnection_Disconnected(string reason) {
            base.NetConnection_Disconnected(reason);
            MessageBox.Show(reason, "连接服务器失败！", MessageBoxButton.OK);
        }
    }
}
